package com.tensquare.auth.controller;

import com.tensquare.auth.service.AuthService;
import com.tensquare.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/9/2-15:56
 * ------------------------------
 */
@Controller
@RequestMapping("oauth2/weibo")
public class AuthController {
    @Autowired
    private AuthService authService;
    /**
     * 功能: 得到用户发送完请求后的code参数并携带此参数获取access_token
     * 参数:
     * 返回值: void
     * 时间: 2021/9/2 15:59
     */
    @GetMapping("/success")
    public String success(String code, HttpSession session) throws Exception {
        //1. 向微博发出请求得到access_token进而得到用户信息,将用户信息保存到数据库中
        User user = authService.sendWeibo(code);
        System.out.println("user = " + user);
        //2. 如果用户存在，保存到session中
        if(user != null){
            session.setAttribute("user",user);
            return "redirect:http://tensquare.com";
        }
        return "redirect:http://tensquare.com/login";
    }
}
